package org.leafstar.shortlink.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.leafstar.shortlink.project.common.convention.result.Result;
import org.leafstar.shortlink.project.common.convention.result.Results;
import org.leafstar.shortlink.project.dto.req.LinkCreateReqDTO;
import org.leafstar.shortlink.project.dto.req.LinkPageReqDTO;
import org.leafstar.shortlink.project.dto.req.LinkUpdateReqDTO;
import org.leafstar.shortlink.project.dto.resp.LinkCreateRespDTO;
import lombok.RequiredArgsConstructor;
import org.leafstar.shortlink.project.dto.resp.LinkGroupCountQueryRespDTO;
import org.leafstar.shortlink.project.dto.resp.LinkPageRespDTO;
import org.leafstar.shortlink.project.service.LinkService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 短链接控制层
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/short-link/v1/project/link")
public class LinkController {
    private final LinkService linkService;

    /**
     * 创建短链接
     * @param requestParam LinkCreateReqDTO
     * @return 创建的结果，比如gid,originLink,fullShortLink等
     */
    @PostMapping("/create")
    public Result<LinkCreateRespDTO> createShortLink(@RequestBody LinkCreateReqDTO requestParam) {
        return Results.success(linkService.createShortLink(requestParam));
    }

    /**
     * 分页查询短链接
     * @param requestParam 带有分页的参数，需要gid,size,current
     * @return page集合
     */
    @GetMapping("/page")
    public Result<IPage<LinkPageRespDTO>> pageShortLink(LinkPageReqDTO requestParam) {
        return Results.success(linkService.pageLink(requestParam));
    }


    /**
     * 查询短链接分组内的链接数量
     * @param gids gids
     * @return LinkGroupCountQueryRespDTO列表
     */
    @GetMapping("/count")
    public Result<List<LinkGroupCountQueryRespDTO>>listGroupShortLinkCount(@RequestParam("gids") List<String> gids){
        return Results.success(linkService.listGroupShortLinkCount(gids));
    }

    /**
     * 更新短链接
     * @param requestParam 请求更新实体
     * @return 更新结果
     */
    @PostMapping("/update")
    public Result<Void>updateShortLink(@RequestBody LinkUpdateReqDTO requestParam){
        linkService.updateShortLink(requestParam);
        return Results.success();
    }
}
